﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="AppManCoEmailRepository.cs" company="DST Nexdox">
//   Copyright (c) DST Nexdox. All rights reserved.
// </copyright>
// <summary>
//   Defines the AppManCoEmailRepository type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace UnityRepository.Repositories
{
  using System;
  using System.Collections.Generic;
  using System.Data.Entity;
  using System.Linq;
  using EFRepository;
  using Entities;
  using UnityRepository.Contexts;
  using UnityRepository.Interfaces;

  public class AppManCoEmailRepository : BaseEfRepository<AppManCoEmail>, IAppManCoEmailRepository
  {
    public AppManCoEmailRepository(String connectionString)
          : base(new UnityDbContext(connectionString))
    {
    }

    public AppManCoEmail GetAppManCoEmail(Int32 id)
    {
      return (from a in Entities where a.Id == id select a).FirstOrDefault();
    }
      
    public IList<AppManCoEmail> GetAppManCoEmails()
    {
      return (from a in this.GetAppManCoEmailsIncludingManCoApplicationAndDocTypeEntities() select a).ToList();
    }

    public IList<AppManCoEmail> GetAppManCoEmails(Int32? appId, Int32? manCoId)
    {
      IQueryable<AppManCoEmail> appManCoEmailsRequest =
        this.GetAppManCoEmailsIncludingManCoApplicationAndDocTypeEntities();

      if (appId.HasValue & appId > 0)
      {
        appManCoEmailsRequest = appManCoEmailsRequest.Where(a => a.ApplicationId == appId);
      }

      if (manCoId.HasValue & manCoId > 0)
      {
        appManCoEmailsRequest = appManCoEmailsRequest.Where(a => a.ManCoId == manCoId);
      }

      return appManCoEmailsRequest.ToList();
    }

    public void UpdateAppManCoEmail(Int32 id, Int32 appId, Int32 manCoId, Int32 docTypeId, String accountNumber, String email, String userName)
    {
      var appManCoEmail = this.GetAppManCoEmail(id);

      var appManCoEmailHistory = new AppManCoEmailHistory
                                 {
                                   ChangeDate = DateTime.Now,
                                   ChangedBy = userName,
                                   ChangeInfo =
                                     String.Format(
                                       "Old ApplicationId : {0}, New ApplicationId: {1}, Old ManCoId: {2}, New ManCoId: {3}, Old DocType: {4}, New DocType: {5}, Old Account Number: {6}, New Account Number {7}, Old Email: {8}, New Email: {9}",
                                       appManCoEmail.ApplicationId,
                                       appId,
                                       appManCoEmail.ManCoId,
                                       manCoId,
                                       appManCoEmail.DocTypeId,
                                       docTypeId,
                                       appManCoEmail.AccountNumber,
                                       accountNumber,
                                       appManCoEmail.Email,
                                       email)
                                 };

      appManCoEmail.ApplicationId = appId;
      appManCoEmail.ManCoId = manCoId;
      appManCoEmail.DocTypeId = docTypeId;
      appManCoEmail.AccountNumber = accountNumber;
      appManCoEmail.Email = email;
      appManCoEmail.AppManCoEmailHistorys.Add(appManCoEmailHistory);

      this.Update(appManCoEmail);
    }

    public PagedResult<AppManCoEmail> GetPagedAppManCoEmails(Int32 pageNumber, Int32 numberOfItems)
    {
      IQueryable<AppManCoEmail> appManCoEmailsRequest =
        this.GetAppManCoEmailsIncludingManCoApplicationAndDocTypeEntities()
        .OrderBy(a => a.Id);

      Int32 totalCount = appManCoEmailsRequest.Count();

      Int32 numItemsToSkip = (pageNumber - 1) * numberOfItems;

      List<AppManCoEmail> appManCoEmails =
        appManCoEmailsRequest
        .OrderBy(a => a.Id)
        .Skip(numItemsToSkip)
        .Take(numberOfItems)
        .ToList();

        return new PagedResult<AppManCoEmail>
        {
            CurrentPage = pageNumber,
            ItemsPerPage = numberOfItems,
            TotalItems = totalCount,
            Results = appManCoEmails.OrderBy(c => c.Id)
            .Skip((pageNumber - 1) * numberOfItems)
            .Take(numberOfItems)
            .ToList(),
            StartRow = numItemsToSkip + 1,
            EndRow = (numItemsToSkip + 1) + (numberOfItems - 1)
        };
    }

    public PagedResult<AppManCoEmail> GetPagedAppManCoEmails(String accountNumber, Int32? appId, Int32? manCoId, Int32 pageNumber, Int32 numberOfItems)
    {
      IQueryable<AppManCoEmail> appManCoEmailsRequest =
        this.GetAppManCoEmailsIncludingManCoApplicationAndDocTypeEntities()
        .OrderBy(a => a.Id);

      if (appId.HasValue & appId > 0)
      {
        appManCoEmailsRequest = appManCoEmailsRequest.Where(a => a.ApplicationId == appId);
      }

      if (manCoId.HasValue & manCoId > 0)
      {
        appManCoEmailsRequest = appManCoEmailsRequest.Where(a => a.ManCoId == manCoId);
      }

      if (!string.IsNullOrEmpty(accountNumber))
      {
        appManCoEmailsRequest = appManCoEmailsRequest.Where(a => a.AccountNumber.Contains(accountNumber));
      }

      Int32 totalCount = appManCoEmailsRequest.Count();

      Int32 numItemsToSkip = (pageNumber - 1) * numberOfItems;

      List<AppManCoEmail> appManCoEmails =
        appManCoEmailsRequest
        .OrderBy(a => a.Id)
        .Skip(numItemsToSkip)
        .Take(numberOfItems)
        .ToList();

      return new PagedResult<AppManCoEmail>
      {
          CurrentPage = pageNumber,
          ItemsPerPage = numberOfItems,
          TotalItems = totalCount,
          Results = appManCoEmails,
          StartRow = numItemsToSkip + 1,
          EndRow = (numItemsToSkip + 1) + (numberOfItems - 1)
      };
    }

    private IQueryable<AppManCoEmail> GetAppManCoEmailsIncludingManCoApplicationAndDocTypeEntities()
    {
      return Entities.Include(x => x.ManCo).Include(x => x.Application).Include(x => x.DocType);
    }
  }
}
